<?php
namespace common\models;


class GroupWinning extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'group_winning';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id',  'commodity_id', 'group_money', 'winning_time', 'member_name', 'member_mobile', 'create_time','winning_time','group_detail_id'], 'required'],
            [['member_id', 'commodity_id', 'is_order', 'create_time'], 'integer'],
            [['group_money'], 'number'],
            [['member_name'], 'string', 'max' => 64],
            [['member_mobile'], 'string', 'max' => 13],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '中奖人员管理表ID',
            'member_id' => '中奖人员ID',
            'commodity_id' => '活动表ID',
            'member_avatar'=>'用户头像',
            'group_money' => '拼团价格',
            'winning_time' => '中奖时间',
            'member_name' => '中奖人员名称',
            'member_mobile' => '中奖人手机号码',
            'is_order' => '状态 1=>已签约 2=》未签约',
            'create_time' => '创建时间',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];
		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];
		
		$data  = $model->asarray()->all();
		if(!$data) return array();

		//扩展信息
		if(!$extends) return $data;

		foreach($extends as $extend)
		{

		}

		return $data;	
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}


	public static function insertAll($idArr,$group_detail_id){

	    $detail = GroupDetail::getOne(['group_detail_id'=>$group_detail_id]);

	    if (!$idArr) return false;
	    foreach ($idArr as $k=>$v){
	        if (!$k){return false;};
	        $id=explode(',',$v);

	        foreach ($id as $ik=>$iv) {

                $member = Member::getOne(['member_id' => $iv], 'member_name,member_mobile,member_avatar');
                $data = array(
                    'member_id' => intval($iv),
                    "commodity_id" => $detail['commodity_id'],
                    "group_detail_id" => $group_detail_id,
                    "group_money" => $detail['group_money'],
                    "winning_time" => date('Y-m-d') . ' ' . $k,
                    "member_name" => $member['member_name'],
                    "member_mobile" => $member['member_mobile'],
                    "member_avatar" => $member['member_avatar'],
                    "is_order" => 2,
                    "create_time" => time()
                );
                $wheres ="source_id=".$group_detail_id." and member_id=".$iv." and source_type=6 and is_lock=1";
                $balanceLog  = BalanceLog::updateAll(['is_await'=>1,'is_lock'=>0],$wheres);

                $model = new GroupWinning();
                $model->load($data, '') && $model->save();

            }

        }


	}

}